/**
 * 登录JS
 * 2008-2-27
 * @author Jacky Chen
 */

/**
 * 登录动作
 * 2008-2-27
 * @author Jacky Chen
 */
function doLogin(){
	DWRUtil.useLoadingMessage("正在登录，请稍候...");
	var selObj = document.getElementsByName("org");
	var selObjValue = "";
	if(selObj.length){
		selObjValue = selObj[selObj.length - 1].value;
	}
	if($("account").value==''){
		alert('用户名不能为空');
		return;
	}
	if($("password").value==''){
		alert('密码不能为空');
		return;
	}
	if($('seccode')){
		if($('seccode').value!=$('seccode_').innerHTML){
			alert('验证码不对');
			$('seccode_').innerHTML=random();
			return;
		}
	}
	FrontAction.doLogin($("account").value,$("password").value,selObjValue,
		function(data){
			location.href = data;
		}
	);
}

/**
 * 展示组织
 * 2008-2-27
 * @author Jacky Chen
 * @param {Object} data
 */
function showOrg(data){
	if(data && data.length > 0){
		var orgDiv = $("org_div");
		var selObj = document.createElement("select");
		selObj.id = "org";
		selObj.name = "org";
		var orgObj = document.getElementsByName("org");
		selObj.level = orgObj.length;
		selObj.onchange = function(){changeOrg(selObj)};
		orgDiv.appendChild(selObj);
		DWRUtil.addOptions(selObj,data,"id","deptname");
		FrontAction.listChildAclorganization(selObj.value,showOrg);	
	}
}

/**
 * 选择组织
 * 2008-2-27
 * @author Jacky Chen
 * @param {Object} changeObj
 */
function changeOrg(changeObj){
	var orgDiv = $("org_div");
	var selObj = document.getElementsByName("org");
	var thisLength = selObj.length;
	for(i=0;i<thisLength;i++){
		if(i>changeObj.level){
			orgDiv.removeChild(selObj[changeObj.level + 1]);
		}
	}
	FrontAction.listChildAclorganization(changeObj.value,showOrg);
}

/**
 * 初始化
 * 2008-2-27
 * @author Jacky Chen
 */
window.onload = function(){
	//FrontAction.listChildAclorganization(0,showOrg);
	var error = JSRequest("error");
	if(error != null && error !=""){
		alert(error);
	}
	$("account").focus();
	if($('seccode_')){
		$('seccode_').innerHTML=random();
	}
	document.documentElement.onkeydown = function(){
		if(event.keyCode == 13){doLogin();}
	}
}

function random(){
	var strs='ab1cd2ef3gh4ij5kl6mn7opq8rst9uvw0xyz',val='';
	for(j=1;j<=4;j++){
		val +=strs.charAt(parseInt(35*Math.random()));
	}
	return val;
}